package com.offcn.controller;

import com.offcn.entity.*;
import com.offcn.service.CardService;
import com.offcn.service.OrderItemService;
import com.offcn.service.OrderService;
import com.offcn.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
public class OrderController {
    @Resource
    private CardService cardService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private ProductService productService;

    //去结算
    @RequestMapping(value = "/order")
    public String orderById(Model model, HttpSession session) {
        List<Trolley> trolleys = cardService.selectByUid(((User) session.getAttribute("user")).getUid());
        model.addAttribute("trolleys", trolleys);
        return "order";
    }

    //显示我的订单
    @RequestMapping(value = "/myOrderByIdAdmin")
    public String myOrderByIdAdmin(HttpSession session, Model model) {
        //根据用户id来进行查询
        List<Order> orderList = orderService.selectById(((User) session.getAttribute("user")).getUid());
        model.addAttribute("orderList", orderList);
        return "myorder";

    }

    //订单详情
    @RequestMapping(value = "/myOrderXiangQing")
    public String myOrderXiangQing(@RequestParam(value = "id", required = false) int id, Model model) {
        Order order = orderService.selectByKeyId(id);

        //查询出订单具体的产品
        List<OrderItem> orderItemList = orderItemService.selectByAll(id);
        model.addAttribute("order", order);
        model.addAttribute("orderItemList", orderItemList);
        return "order_detail";
    }

    //  提交订单插入订单信息

    @RequestMapping(value = "/orderByUid")
    public String orderById(HttpSession session, @RequestParam(value = "sum", required = false) String sum,
                            @RequestParam(value = "addressName", required = false) String addressName,
                            @RequestParam(value = "tel", required = false) String tel,
                            @RequestParam(value = "address", required = false) String address,
                            @RequestParam(value = "mask", required = false) String mask) {
        // 插入到数据库里
        Order order = new Order();
        order.setUid(((User) session.getAttribute("user")).getUid());
        order.setXs_address(address);
        order.setXs_fsum(Float.parseFloat(sum));
        order.setXs_tel(tel);
        order.setXs_name(addressName);
      //  order.setOrderStateId("XS" + System.currentTimeMillis());
        order.setStatus("1");
        order.setXs_date(new Date());


        int num = orderService.insertOrder(order);
        List<Product> productList = cardService.selectAll(((User) session.getAttribute("user")).getUid());
        for (int i = 0; i < productList.size(); i++) {
            OrderItem orderItem = new OrderItem();
          //  orderItem.setCount(1);
            orderItem.setPid(productList.get(i).getId());
            //orderItem.setVip(1);
           // orderItem.setOid(order.getBoid());
            orderItem.setSubtotal(productList.get(i).getPrice());
            orderItemService.insertOrderItem(orderItem);

        }
        session.setAttribute("order", order);
        session.setAttribute("sum", sum);
        //清空购物车
        // cardService.deleteAll();
        //插入子的条目

        //System.out.println("***********************"+order.getBoid());

        //System.out.println(sum+"\t"+tel+"\t"+address+"\t"+addressName+"\t"+mask);
        return "redirect:/pay";
    }

    @RequestMapping(value = "pay")
    public  String  pay(){
       // cardService.deleteAll(); 胡说
        return "pay/index";
    }

    @RequestMapping(value = "zhifu")
    public   String    zhifu(HttpSession session,@RequestParam(value = "WIDout_trade_no",required = false)String WIDout_trade_no,
                             @RequestParam(value = "WIDtotal_amount",required = false)String WIDtotal_amount){
        session.setAttribute("WIDout_trade_no",WIDout_trade_no);
        session.setAttribute("WIDtotal_amount",WIDtotal_amount);
        session.setAttribute("WIDsubject","机械产品");
        return "pay/alipay.trade.page.pay";

    }

}
